/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uk.co.dupenois.geometry;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author mpd209
 */
public class CoordinateListOld {
    private Vector<Coordinate> pointsByX;
    private Vector<Coordinate> pointsByY;
    private Vector<Coordinate> pointsByXY;
    private Vector<Coordinate> pointsByMinXMaxY;
    public CoordinateListOld(Coordinate[] coordinates){
        Vector<Coordinate> coords = new Vector<Coordinate>();
        for(Coordinate c : coordinates) coords.add(c);
        initialise(coords);
    }
    public CoordinateListOld(Vector<Coordinate> coordinates){
        initialise(coordinates);
    }
    private void initialise(Vector<Coordinate> coordinates){
        pointsByX = (Vector<Coordinate>)coordinates.clone();
        pointsByY = (Vector<Coordinate>)coordinates.clone();
        pointsByXY = (Vector<Coordinate>)coordinates.clone();
        pointsByMinXMaxY = (Vector<Coordinate>)coordinates.clone();
        sortByX(pointsByX);
        sortByY(pointsByY);
        sortByXY(pointsByXY);
        sortByMinXMaxY(pointsByMinXMaxY);
    }

    public int size(){
        return pointsByX.size();
    }

    public Coordinate getFromXList(int index){
        return pointsByX.get(index);
    }

    public Coordinate getFromYList(int index){
        return pointsByY.get(index);
    }

    public Iterator<Coordinate> xIterator(){
        return pointsByX.iterator();
    }


    public Iterator<Coordinate> yIterator(){
        return pointsByY.iterator();
    }

    public Iterator<Coordinate> xyIterator(){
        return pointsByXY.iterator();
    }
     public Iterator<Coordinate> minXMaxYIterator(){
        return pointsByMinXMaxY.iterator();
    }


    private void sortByX(Vector<Coordinate> points){
        Collections.sort(points, new Comparator<Coordinate>() {
            public int compare(Coordinate arg0, Coordinate arg1) {
                double diff = arg0.getX() - arg1.getX();
                if(diff == 0){
                    diff = arg1.getY() - arg0.getY();
                    if(diff == 0) return 0;
                    if(diff<0) return -1;
                    return 1;
                }
                if(diff<0)return -1;
                return 1;
            }
        });
    }
    private void sortByY(Vector<Coordinate> points){
        Collections.sort(points, new Comparator<Coordinate>() {
            public int compare(Coordinate arg0, Coordinate arg1) {
                double diff = arg0.getY() - arg1.getY();
                if(diff == 0){
                    diff = arg1.getX() - arg0.getX();
                    if(diff == 0) return 0;
                    if(diff<0) return -1;
                    return 1;
                }
                if(diff<0)return -1;
                return 1;
            }
        });
    }
    private void sortByXY(Vector<Coordinate> points){
        Collections.sort(points, new Comparator<Coordinate>() {
            public int compare(Coordinate arg0, Coordinate arg1) {
                double diff = (Math.pow(arg0.getX(), 2)+Math.pow(arg0.getY(), 2))-
                             (Math.pow(arg1.getX(), 2)+Math.pow(arg1.getY(), 2));
                if(diff == 0)return 0;
                if(diff<0)return -1;
                return 1;
            }
        });
    }
    private void sortByMinXMaxY(Vector<Coordinate> points){
        Collections.sort(points, new Comparator<Coordinate>() {
            public int compare(Coordinate arg0, Coordinate arg1) {
                Coordinate topLeft = getBoxTopLeft();
                double dist1 = Math.pow((arg0.getX()-topLeft.getX()), 2)+
                        Math.pow((arg0.getY()-topLeft.getY()), 2);
                double dist2 = Math.pow((arg1.getX()-topLeft.getX()), 2)+
                        Math.pow((arg1.getY()-topLeft.getY()), 2);
                double diff = dist1 - dist2;
                if(diff == 0)return 0;
                if(diff<0)return -1;
                return 1;
//
//                double ydiff = arg0.getY() - arg1.getY();
//                double xdiff = arg0.getX() - arg1.getX();
//                if(xdiff<0 && ydiff>0)return -1;
//                if(xdiff==0 && ydiff==0)return 0;
//                if(xdiff>0 && ydiff<0)return 1;
//                if(xdiff<0 && ydiff<0)return 1;
//
////                if(xdiff+ydiff<0)return -1;
////                if(xdiff+ydiff==0)return 0;
//                return 1;

            }
        });
    }
    public Coordinate getBoxTopLeft(){
        return new Coordinate(getFirstFromX().getX(), getLastFromY().getY());
    }
    public Coordinate getBoxTopRight(){
        return new Coordinate(getLastFromX().getX(), getLastFromY().getY());
    }
    public Coordinate getBoxBottomRight(){
        return new Coordinate(getLastFromX().getX(), getFirstFromY().getY());
    }
    public Coordinate getBoxBottomLeft(){
        return new Coordinate(getFirstFromX().getX(), getFirstFromY().getY());
    }
    public double getMinXValue(){
        return getFirstFromX().getX();
    }
    public double getMaxYValue(){
        return getLastFromY().getY();
    }
    public Coordinate getFirstFromX(){
        return pointsByX.firstElement();
    }
    public Coordinate getFirstFromY(){
        return pointsByY.firstElement();
    }
    public Coordinate getLastFromX(){
        return pointsByX.lastElement();
    }
    public Coordinate getLastFromY(){
        return pointsByY.lastElement();
    }
    public Vector<Coordinate> getByX(){
        return pointsByX;
    }
    public Vector<Coordinate> getByY(){
        return pointsByY;
    }


    public CoordinateListOld[] splitByX(){
        int size = this.size();
        int middle = (int)Math.floor((double)size/2d);
        CoordinateListOld[] arr = new CoordinateListOld[2];
        Vector<Coordinate> c1 = new Vector<Coordinate>();
        Vector<Coordinate> c2 = new Vector<Coordinate>();
        c1.addAll(pointsByX.subList(0, middle));
        c2.addAll(pointsByX.subList(middle, size));
        arr[0] = new CoordinateListOld(c1);
        arr[1] = new CoordinateListOld(c2);
        return arr;
    }
    public CoordinateListOld[] splitByY(){
        int size = this.size();
        int middle = (int)Math.floor((double)size/2d);
        CoordinateListOld[] arr = new CoordinateListOld[2];
        Vector<Coordinate> c1 = new Vector<Coordinate>();
        Vector<Coordinate> c2 = new Vector<Coordinate>();
        c1.addAll(pointsByY.subList(0, middle));
        c2.addAll(pointsByY.subList(middle, size));
        arr[0] = new CoordinateListOld(c1);
        arr[1] = new CoordinateListOld(c2);
        return arr;
    }
}
